package cn.amarone.common.core.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author Amarone
 * @version 1.0
 * @description: 响应信息主体
 * @date 2022/9/14 15:34
 */
@ToString
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = -4090994942799958083L;

    public static final int SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "SUCCESS";

    public static final int FAIL_CODE = 500;
    public static final String FAIL_MSG = "FAIL";

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;


    public static <T> R<T> ok() {
        return new R(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static <T> R<T> ok(T data) {
        return new R(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> R<T> ok(String msg) {
        return new R(SUCCESS_CODE, msg, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return new R(SUCCESS_CODE, msg, data);
    }

    public static <T> R<T> fail() {
        return new R(FAIL_CODE, FAIL_MSG, null);
    }

    public static <T> R<T> fail(T data) {
        return new R(FAIL_CODE, FAIL_MSG, data);
    }

    public static <T> R<T> fail(String msg) {
        return new R(FAIL_CODE, msg, null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return new R(FAIL_CODE, msg, data);
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}
